package com.cpi.cornpp.costelement.action;

import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.filter.BreadCrumbList;
import com.cpi.cornpp.costelement.exception.CostElementException;
import com.cpi.cornpp.costelement.form.CostElementForm;
import com.cpi.cornpp.costelement.view.CostElementView;
import com.cpi.cornpp.transports.service.TransportService;
import com.cpi.cornpp.transports.view.TransportView;
import com.cs.common.init.beans.SiteMapDTO;

public class ShowAddCostElementTransportAction extends Action {
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	private static final String SUCCESS_FORWARD = "toSuccess";
	private static final String FAILURE_FORWARD = "error";
	private static final String TRANSPORT_LIST_EMPTY = "error.message.transports.cost.list.empty";//$NON-NLS-1$
	private static final String COST_ELEMENT_LIST_FORWARD = "costElementList";
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse respose) throws CostElementException{
		String messageError = null;
		CostElementForm costElementForm = null;
		CostElementView costElementView = null;
		TransportService transportService = null;
		List transportsView = null;
		HttpSession session = null;
		ActionForward forward = null;
		MessageResources messageResources = null;
		
		forward = mapping.findForward(FAILURE_FORWARD);
		
		try{
			
			session = request.getSession(false);
			messageResources = getResources(request);
			costElementView = (CostElementView) session.getAttribute("costElementView");
			
			if ((form != null) && (costElementView != null)){
				costElementForm = (CostElementForm) form;
				transportService = new TransportService();
				transportsView = transportService.getTransports();
				
				costElementView.setCostElementCode(costElementForm.getCostElementView().getCostElementCode());
				costElementView.setCostElementDescription(costElementForm.getCostElementView().getCostElementDescription());
				
				removeAddedCosts(transportsView,costElementView.getTransports());
				if( transportsView.size() == 0 ){
					forward = mapping.findForward((costElementForm.getCostElementView().getCostElementID() != -1) ? "showEditCostElement" : "showAddCostElement");
					session.setAttribute(ERROR_MESSAGE, messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), TRANSPORT_LIST_EMPTY));
				}else{
					session.setAttribute("transportsList", transportsView);
					session.setAttribute("costElementView", costElementView);
					request.setAttribute("transportsList", transportsView);
					request.setAttribute("backLink", ((costElementForm.getCostElementView().getCostElementID() != -1) ? "showEditCostElement.do" : "showAddCostElement.do"));
					
					forward = mapping.findForward(SUCCESS_FORWARD);
				}
				
				//FIXME
				BreadCrumbList breadCrumbList = new BreadCrumbList(0);
				SiteMapDTO siteMapDTO = new SiteMapDTO();
				
				siteMapDTO.setLabel("label.breadcrum.home");
				siteMapDTO.setUrl("welcome.do");
				breadCrumbList.add(siteMapDTO);
				
				siteMapDTO = new SiteMapDTO();
				
				siteMapDTO.setLabel("label.breadcrum.transport.list");
				siteMapDTO.setUrl("showTransportQuery.do");
				breadCrumbList.add(siteMapDTO);
				
				siteMapDTO = new SiteMapDTO();
				
				siteMapDTO.setLabel(((costElementForm.getCostElementView().getCostElementID() != -1) ? "label.breadcrum.costElement.edit" : "label.breadcrum.costElement.new"));
				siteMapDTO.setUrl(((costElementForm.getCostElementView().getCostElementID() != -1) ? "showEditCostElement.do" : "showAddCostElement.do"));
				breadCrumbList.add(siteMapDTO);
				
				breadCrumbList.setLabel("label.breadcrum.costElement.add");
				
				session.setAttribute("BREADCRUMBS", breadCrumbList);
				session.setAttribute("BREADCRUMB_LABEL", breadCrumbList.getLabel());

			} else {
				forward = mapping.findForward(COST_ELEMENT_LIST_FORWARD);
			}

		} catch(CostElementException cee){
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), cee.getErrorCode());
			
			if(null == messageError){
				session.setAttribute(CUSTOM_EXCEPTION, cee);
				form.reset(mapping, request);
				throw cee;
			}else{
				session.setAttribute(ERROR_MESSAGE, messageError);
				forward = mapping.findForward(FAILURE_FORWARD);
			}
			
		} catch(Exception e){
			CostElementException cee = new CostElementException(e , CostElementException.LAYER_ACTION, CostElementException.ACTION_LISTS);
			
			session.setAttribute(CUSTOM_EXCEPTION, cee);
			form.reset(mapping, request);
			e.printStackTrace(System.out);
			
			throw cee;
		}
		
		return forward;
	}
	
	private void removeAddedCosts(List costElementsView, List costElementsAdded)throws CostElementException{
		Iterator iteratorAdded = null;
		Iterator iteratorViews = null;
		TransportView costElementViewAdded = null;
		TransportView costElementView = null;
		int int_i = 0;
		try{
			if( costElementsAdded != null ){
				iteratorAdded = costElementsAdded.iterator();
				while(iteratorAdded.hasNext()){
					int_i = 0;
					costElementViewAdded = (TransportView)iteratorAdded.next();
					iteratorViews = costElementsView.iterator();
					while( iteratorViews.hasNext() ){
						costElementView = (TransportView)iteratorViews.next();
						if( costElementView.getTransportShortName().equals(costElementViewAdded.getTransportShortName())){
							costElementsView.remove(int_i);
							break;
						}
						int_i++;
					}
				}
			}
		}catch(Exception exception){
			CostElementException cee = new CostElementException(exception, CostElementException.LAYER_ACTION, CostElementException.ACTION_SELECT);
			throw cee;
		}
	}
}